<template>
    <IDrawer v-model="defaultValue" :loading="loading" :width="650" title="加挂订单">
        <Form :model="data" :label-width="100" :rules="ruleValidate" ref="formCreate">
            <Alert banner v-if="errMessage !== ''" type="error">{{errMessage}}</Alert>
            <FormItem label="物流单号" prop="express_code" :showMessage="false">
                <Input v-model="data.express_code" @on-enter="codeEnter" ref="express_code">
                    <Button slot="append" @click="codeEnter">下一步</Button>
                </Input>
            </FormItem>
            <FormItem label="重量" prop="weight" :showMessage="false">
                <Input v-model="data.weight" number ref="weight" @on-enter="submit('formCreate')">
                    <Button slot="append" @click="submit('formCreate')">提交</Button>
                </Input>
            </FormItem>
        </Form>
    </IDrawer>
</template>

<script>
    import IForm from "../../../components/content/form";
    import contentDrawer from "../../../mixins/contentDrawer";
    import IDrawer from "../../../components/content/modal";
    import RemoteSelect from "../../../components/form/remoteSelect";

    export default {
        name: "update",
        mixins: [contentDrawer],
        components: {RemoteSelect, IForm, IDrawer},
        data() {
            return {
                ruleValidate: {
                    express_code: [{required: true, message: '物流单号必须填写'}],
                    weight: [{required: true, type: 'number', message: '重量必须填写'}],
                },
                data: {},
                errMessage: ''
            }
        },
        mounted() {
            this.$nextTick(() => {
                this.restart();
            });
        },
        methods: {
            codeEnter() {
                this.$refs['weight'].focus();
                this.errMessage = '';
            },
            submit(data) {
                this.validate(data).then(() => {
                    this.loading = true;
                    this.$http.put(`order/shipmentMain/${this.props.id}/checking`, this.data).then((res) => {
                        if (res.status === 1) {
                            this.submitPost();
                        } else {
                            this.$Modal.confirm({
                                title: "确认",
                                content: `系统计算的重量 ${res.sys_weight} ;称重 ${this.data.weight} 。`,
                                "lock-scroll": true,
                                onOk: () => {
                                    this.submitPost();
                                }
                            });
                        }
                    }).catch(() => {

                    }).finally(() => {
                        this.loading = false;
                    });
                });
            },
            submitPost() {
                this.loading = true;
                this.$http.put(`order/shipmentMain/${this.props.id}`, this.data).then(() => {
                    this.data = {};
                    this.$refs['express_code'].focus();
                }).catch((err) => {
                    this.errMessage = err.data.message
                }).finally(() => {
                    this.loading = false;
                    this.restart();
                });
            },
            restart() {
                this.data = {};
                this.$refs['express_code'].focus();
            }
        }
    }
</script>

<style scoped>

</style>
